/**@file
   I/O class implement.
   
   @par license 
   Copyright 2012 Lu, Ken. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 
   @author: Lu, Ken (bluewish.ken.lu@gmail.com)
**/

#include "io.h"

#ifdef _MSC_VER
#include "kstub.h"
#endif // _MSC_VER

namespace ken {

#ifdef _MSC_VER

//
// Implement class
//
class u_IO::Impl
{
public:
    u_IO::Impl::Impl() {}
    u_IO::Impl::~Impl() {}

    u8_t read8(u16_t port) {
        return (u8_t)u_KStubDev::instance()->ioread(port, 1);
    }

    u16_t read16(u16_t port) {
        return (u16_t)u_KStubDev::instance()->ioread(port, 2);
    }

    u32_t read32(u16_t port) {
        return (u32_t)u_KStubDev::instance()->ioread(port, 4);
    }

    void  write8(u16_t port, u8_t in) {
        u32_t value = (u32_t)in;
        u_KStubDev::instance()->iowrite(port, 1, value);
    }

    void  write16(u16_t port, u16_t in) {
        u32_t value = (u32_t)in;
        u_KStubDev::instance()->iowrite(port, 2, value);
    }

    void  write32(u16_t port, u32_t in) {
        u_KStubDev::instance()->iowrite(port, 4, in);
    }

private:
    DISALLOW_COPY_AND_ASSIGN(Impl);
};

#endif // _MSC_VER
//
// TODO: implement linux verion later by using inw_p, inc_p, outc_p, outw_p.
//

//
// u_UI class
//
u_IO::u_IO():pImpl_(new Impl()) {
}

u_IO::~u_IO() {
    delete pImpl_;
}

u8_t u_IO::read8(u16_t port)
{
    return pImpl_->read8(port);
}

u16_t u_IO::read16(u16_t port)
{
    return pImpl_->read16(port);
}

u32_t u_IO::read32(u16_t port)
{
    return pImpl_->read32(port);
}

void u_IO::write8(u16_t port, u8_t in)
{
    pImpl_->write8(port, in);
}

void u_IO::write16(u16_t port, u16_t in)
{
    pImpl_->write16(port, in);
}

void u_IO::write32(u16_t port, u32_t in)
{
    pImpl_->write32(port, in);
}

}
